package com.cslg.questionnaires.mapper;

import com.cslg.questionnaires.entity.QuestionForm;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface QuestionFormMapper {
    @Select("SELECT count(*) FROM questionform WHERE questionid = #{questionId}")
    Integer getCountFormByQuestionId(int questionId);

    @Select("SELECT questionform.id formId,question.id questionId,title,stu_id,stu_name,major_name " +
            "FROM question,t_student,t_clazz,questionform " +
            "WHERE questionid = question.id AND studentid = t_student.id " +
            "AND t_student.clazz_name = t_clazz.clazz_name AND owerid = #{userId} " +
            "AND owertype = #{userType} ORDER by questionform.id DESC")
    List<Map> getAllFormByUid_type(int userId, String userType);

    @Select("SELECT title,stu_id,stu_name,major_name,questionform.content answerContent,question.content formContent " +
            "FROM question,t_student,t_clazz,questionform " +
            "WHERE questionid = question.id AND studentid = t_student.id " +
            "AND t_student.clazz_name = t_clazz.clazz_name AND questionform.id = #{formid} " +
            "AND owerid = #{userid} AND owertype = #{usertype}")
    Map<String,Object> getFormByFormId(int formid,int userid,String usertype);

    @Select("SELECT questionform.id formId,question.id questionId,title,stu_id,stu_name,major_name " +
            "FROM question,t_student,t_clazz,questionform " +
            "WHERE questionid = question.id AND studentid = t_student.id " +
            "AND t_student.clazz_name = t_clazz.clazz_name AND owerid = #{userId} " +
            "AND owertype = #{userType} AND questionid = #{questionId} ORDER by questionform.id DESC")
    List<Map> getFormByQuestionId(int questionId,int userId, String userType);

    @Select("SELECT content FROM questionform WHERE questionid = #{questionId}")
    List<Map> getFormContentByQuestionId(int questionId);

    @Select("SELECT * FROM questionform WHERE questionid = #{questionId} AND studentid = #{studentId}")
    QuestionForm getFormByQuStuId(int questionId,int studentId);

    @Insert("INSERT INTO questionform(questionid,studentid,content) VALUES(#{questionid},#{studentid},#{content})")
    Integer submitQuestionForm(QuestionForm form);

    @Update("UPDATE questionform SET del = 1 WHERE questionid = #{questionId}")
    Integer delQuestionFormById(int questionId);
}
